//
//  HomeBannerTableViewCell.m
//  Ev_Master
//
//  Created by Evan on 2019/6/11.
//  Copyright © 2019 Evan. All rights reserved.
//

#import "HomeBannerTableViewCell.h"
#import "NewPagedFlowView.h"

#define Width [UIScreen mainScreen].bounds.size.width

@interface HomeBannerTableViewCell()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>

@property(nonatomic, strong) NewPagedFlowView *pageFlowView ;

@end

@implementation HomeBannerTableViewCell


+ (instancetype)cellWithTableView:(UITableView *)tableView  IndexPath:(NSIndexPath *)indexPath{
    NSString *Service = [NSString stringWithFormat:@"HomeBannerTableViewCell_%ld_%ld",indexPath.section,indexPath.row];
    HomeBannerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Service];
    if (cell == nil) {
        [ tableView registerClass:[HomeBannerTableViewCell class] forCellReuseIdentifier:Service];
        cell = [tableView dequeueReusableCellWithIdentifier:Service];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell initUI];
    }
    return cell;
}

-(void)initUI{
    
    self.pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, 0, Width, Width * 9 / 16)];
    self.pageFlowView.delegate = self;
    self.pageFlowView.dataSource = self;
    self.pageFlowView.minimumPageAlpha = 0.1;
    self.pageFlowView.isCarousel = YES;
    self.pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    self.pageFlowView.isOpenAutoScroll = YES;
    self.pageFlowView.autoTime = 3.0;
    
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.pageFlowView.frame.size.height - 32, Width, 8)];
    self.pageFlowView.pageControl = pageControl;
    [self.pageFlowView addSubview:pageControl];
    [self.pageFlowView reloadData];
    
    [self addSubview:self.pageFlowView];
    
    //添加到主view上
    //        [self addSubview:self.indicateLabel];
}


-(void)setImageArray:(NSMutableArray *)imageArray{
    _imageArray = imageArray;
    [self.pageFlowView reloadData];
}



#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(Width - 60, (Width - 60) * 7 / 16);
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
    
    //    self.indicateLabel.text = [NSString stringWithFormat:@"点击了第%ld张图",(long)subIndex + 1];
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
//    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
    self.blockPageNumber(pageNumber);
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.imageArray.count;
    
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = [flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] init];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    //在这里下载网络图片
    //  [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:hostUrlsImg,imageDict[@"img"]]] placeholderImage:[UIImage imageNamed:@""]];
    bannerView.mainImageView.image = self.imageArray[index];
    
    return bannerView;
}


@end
